package com.shelpe.services.scheduler.item.customer;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shelpe.services.scheduler.dto.RptCustomerDto;
import com.shelpe.services.scheduler.mongorepository.UnrecoverableErrorRepository;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.po.UnrecoverableError;
import com.shelpe.services.scheduler.po.Users;
import com.shelpe.services.scheduler.syncclient.CustomerClient;
import com.shelpe.services.scheduler.util.Util;

@Component("userProcessor")
public class UserProcessor implements ItemProcessor<Users, SyncLog>{

	@Autowired
	private CustomerClient cusClient;
	
	@Autowired
	private UnrecoverableErrorRepository errorRep;
	
	
	@Override
	public SyncLog process(Users item) throws Exception {
		// TODO Auto-generated method stub
		
		SyncLog log = null;
			
		long userId = item.getUserId();
		RptCustomerDto rptDto = cusClient.syncUserBalance(userId);
		//对于返回的特定信息，记录db
		if(rptDto != null && rptDto.getMessage() != null
				&& (rptDto.getMessage().equals("invalid-sessionkey") || rptDto.getMessage().equals("无法根据nick获取直通车帐号信息"))){
			
			UnrecoverableError error = new UnrecoverableError();
			error.setUserId(userId);
			error.setError(rptDto.getMessage());
			errorRep.save(error);
		}else{
			errorRep.delete(userId);
		}
		
		log = Util.convert(rptDto);
		
		return log;
	}
}
